using Cinemachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DungeonMap : SingletonMonobehaviour<DungeonMap>
{
    #region Header GameObject References
    [Space(10)]
    [Header("游戏对象引用")]
    #endregion
    #region Tooltip
    [Tooltip("填充 MinimapUI 游戏对象")]
    #endregion
    [SerializeField] private GameObject minimapUI;
    private Camera dungeonMapCamera;
    private Camera cameraMain;

    private void Start()
    {
        // 缓存主摄像头
        cameraMain = Camera.main;

        // 获取播放器转换
        Transform playerTransform = GameManager.Instance.GetPlayer().transform;

        // 将播放器填充为 cinemachine 摄像机目标
        CinemachineVirtualCamera cinemachineVirtualCamera = GetComponentInChildren<CinemachineVirtualCamera>();
        cinemachineVirtualCamera.Follow = playerTransform;

        // 获取DungeonMap相机
        dungeonMapCamera = GetComponentInChildren<Camera>();
        dungeonMapCamera.gameObject.SetActive(false);
    }

    private void Update()
    {
        // 如果按下鼠标按钮并且游戏状态为地牢概览图，则点击房间
        if (Input.GetMouseButtonDown(0) && GameManager.Instance.gameState == GameState.dungeonOverviewMap)
        {
            GetRoomClicked();
        }
    }

    /// <summary>
    /// 在地图上点击房间
    /// </summary>
    private void GetRoomClicked()
    {
        // 将屏幕位置转换为世界位置
        Vector3 worldPosition = dungeonMapCamera.ScreenToWorldPoint(Input.mousePosition);
        worldPosition = new Vector3(worldPosition.x, worldPosition.y, 0f);

        // 检查光标位置是否发生冲突
        Collider2D[] collider2DArray = Physics2D.OverlapCircleAll(new Vector2(worldPosition.x, worldPosition.y), 1f);

        // 检查是否有任何碰撞体是一个房间
        foreach (Collider2D collider2D in collider2DArray)
        {
            if (collider2D.GetComponent<InstantiatedRoom>() != null)
            {
                InstantiatedRoom instantiatedRoom = collider2D.GetComponent<InstantiatedRoom>();

                // 如果点击的房间没有敌人并且之前访问过，则将玩家移动到房间
                if (instantiatedRoom.room.isClearedOfEnemies && instantiatedRoom.room.isPreviouslyVisited)
                {
                    // 将玩家移动到房间
                    StartCoroutine(MovePlayerToRoom(worldPosition, instantiatedRoom.room));
                }
            }
        }

    }

    /// <summary>
    /// 将播放器移动到选定的房间
    /// </summary>
    private IEnumerator MovePlayerToRoom(Vector3 worldPosition, Room room)
    {
        // 呼叫室更改事件
        StaticEventHandler.CallRoomChangedEvent(room);

        //立即淡出屏幕变黑
        yield return StartCoroutine(GameManager.Instance.Fade(0f, 1f, 0f, Color.black));

        // 清除地牢概览
        ClearDungeonOverViewMap();

        // 在淡入淡出期间禁用播放器
        GameManager.Instance.GetPlayer().playerControl.DisablePlayer();

        // 在离玩家最近的房间中获取最近的出生点
        Vector3 spawnPosition = HelperUtilities.GetSpawnPositionNearestToPlayer(worldPosition);

        // 将玩家移动到新位置 - 在最近的生成点生成他们
        GameManager.Instance.GetPlayer().transform.position = spawnPosition;

        // 将屏幕重新淡入
        yield return StartCoroutine(GameManager.Instance.Fade(1f, 0f, 1f, Color.black));

        // 启用播放器
        GameManager.Instance.GetPlayer().playerControl.EnablePlayer();
    }


    /// <summary>
    /// 显示地牢总览图 UI
    /// </summary>
    public void DisplayDungeonOverViewMap()
    {
        // 设置游戏状态
        GameManager.Instance.previousGameState = GameManager.Instance.gameState;
        GameManager.Instance.gameState = GameState.dungeonOverviewMap;

        // 禁用播放器
        GameManager.Instance.GetPlayer().playerControl.DisablePlayer();

        // 禁用主摄像头并启用地牢概览摄像头
        cameraMain.gameObject.SetActive(false);
        dungeonMapCamera.gameObject.SetActive(true);

        // 确保所有房间都处于活动状态，以便可以显示它们
        ActivateRoomsForDisplay();

        // 禁用小型小地图用户界面
        minimapUI.SetActive(false);
    }

    /// <summary>
    /// 清除地牢总览图 UI
    /// </summary>
    public void ClearDungeonOverViewMap()
    {
        // 设置游戏状态
        GameManager.Instance.gameState = GameManager.Instance.previousGameState;
        GameManager.Instance.previousGameState = GameState.dungeonOverviewMap;

        // 启用播放器
        GameManager.Instance.GetPlayer().playerControl.EnablePlayer();

        // 启用主摄像头并禁用地牢概览摄像头
        cameraMain.gameObject.SetActive(true);
        dungeonMapCamera.gameObject.SetActive(false);

        // 启用小型小地图用户界面
        minimapUI.SetActive(true);
    }

    /// <summary>
    /// 确保所有房间都处于活动状态，以便可以显示它们
    /// </summary>
    private void ActivateRoomsForDisplay()
    {
        // 遍历地牢房间
        foreach (KeyValuePair<string, Room> keyValuePair in DungeonBuilder.Instance.dungeonBuilderRoomDictionary)
        {
            Room room = keyValuePair.Value;

            room.instantiatedRoom.gameObject.SetActive(true);
        }
    }
}